#include "Frm02.h"

Frm02::Frm02(HmiApp* app, STTWidget *par, const string& id, const string& desc) : STTPage(app->getRuntime(), par, id, desc) {
	hmiApp = app;
}

Frm02::~Frm02() {
}

void Frm02::onInit() {

}

void Frm02::createParts() {
	setMainArg(0, 0, 1024, 600);
	setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 0, 0xc0c0c0);

	/*----------------------------- Button : wMButton1                   -----------------------------*/
	wMButton1 = new STTButton(p_rt, this, "wMButton1", 0);
	wMButton1->setMainArg(1, 1, 198, 48, false);
	wMButton1->setPressedArg("返回主页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->setReleasedArg("返回主页", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->bindOpenFormHandler(Event::CLICKED, "Frm01", PageMode::NEWWIN, NULL);

	/*----------------------------- Button : wMButton2                   -----------------------------*/
	wMButton2 = new STTButton(p_rt, this, "wMButton2", 0);
	wMButton2->setMainArg(200, 1, 198, 48, false);
	wMButton2->setPressedArg("Discovery", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->setReleasedArg("Discovery", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->onEventHandler(Event::CLICKED, this, (EHandler) & Frm02::wMButton2_clk_cb);

	/*----------------------------- Text : wMText1                       -----------------------------*/
	wMText1 = new STTTextArea(p_rt, this, "wMText1");
	wMText1->setMainArg(4, 106, 606, 46, false, true, TextAlign::CENTER, -1, -1, -99999999, 99999999, false);
	wMText1->setMainStyle(7, 0xffffff, 0xffffff, 0, 2, 0xc0c0c0, 9, 10, 4, 4, 255);
	wMText1->setPlaceholderText("")->setMaxLength(0)->setAcceptedChars("")->setImeMode(IME::UC);
	wMText1->setFont(0x0, 16, "文泉驿微米黑", 0);

	/*----------------------------- Button : wMButton3                   -----------------------------*/
	wMButton3 = new STTButton(p_rt, this, "wMButton3", 0);
	wMButton3->setMainArg(399, 1, 198, 48, false);
	wMButton3->setPressedArg("continuousMove", 0x0, "文泉驿微米黑", 16, 0);
	wMButton3->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton3->setReleasedArg("continuousMove", 0x0, "文泉驿微米黑", 16, 0);
	wMButton3->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton3->onEventHandler(Event::CLICKED, this, (EHandler) & Frm02::wMButton3_clk_cb);

	/*----------------------------- Slider : wMSlider1                   -----------------------------*/
	wMSlider1 = new STTSlider(p_rt, this, "wMSlider1");
	wMSlider1->setMainArg(699, 21, 164, 22, 0, -100, 100, false);
	wMSlider1->setMainStyle(9999, 0xa0a0a0, 0xffffff, 0, 0, 0xa0a0a0, 0, 0x0, NULL);
	wMSlider1->setIndicStyle(9999, 0x1a2b1, 0xffffff, 0, 0, 0x1a2b1, 0, 0xffffff, NULL);
	wMSlider1->setKnobStyle(7, 0x1a2b1, 0xffffff, 0, 0, 0x1a2b1, 0, 0x0, NULL, 7);

	/*----------------------------- Slider : wMSlider2                   -----------------------------*/
	wMSlider2 = new STTSlider(p_rt, this, "wMSlider2");
	wMSlider2->setMainArg(699, 56, 164, 22, 0, -100, 100, false);
	wMSlider2->setMainStyle(9999, 0xa0a0a0, 0xffffff, 0, 0, 0xa0a0a0, 0, 0x0, NULL);
	wMSlider2->setIndicStyle(9999, 0x1a2b1, 0xffffff, 0, 0, 0x1a2b1, 0, 0xffffff, NULL);
	wMSlider2->setKnobStyle(7, 0x1a2b1, 0xffffff, 0, 0, 0x1a2b1, 0, 0x0, NULL, 7);

	/*----------------------------- Slider : wMSlider3                   -----------------------------*/
	wMSlider3 = new STTSlider(p_rt, this, "wMSlider3");
	wMSlider3->setMainArg(699, 91, 164, 22, 0, -100, 100, false);
	wMSlider3->setMainStyle(9999, 0xa0a0a0, 0xffffff, 0, 0, 0xa0a0a0, 0, 0x0, NULL);
	wMSlider3->setIndicStyle(9999, 0x1a2b1, 0xffffff, 0, 0, 0x1a2b1, 0, 0xffffff, NULL);
	wMSlider3->setKnobStyle(7, 0x1a2b1, 0xffffff, 0, 0, 0x1a2b1, 0, 0x0, NULL, 7);

	/*----------------------------- Label : wMLabel1                     -----------------------------*/
	wMLabel1 = new STTLabel(p_rt, this, "wMLabel1");
	wMLabel1->setMainArg(4, 65, 594, 26, "标签", TextAlign::LEFT, LabelLongMode::CLIP, 30);
	wMLabel1->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel1->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel1->setFormatText(TextAlign::LEFT, LabelLongMode::CLIP, 30);

	/*----------------------------- Video : wMVideo1                     -----------------------------*/
	wMVideo1 = new STTFFmpeg(p_rt, this, "wMVideo1");
	wMVideo1->channel = 0;
	wMVideo1->setRotate(0);
	wMVideo1->setMainArg(10, 158, 1008, 436, "");
	wMVideo1->setAutoRestart(true);
	wMVideo1->setVolume(50);

	/*----------------------------- Button : wMButton4                   -----------------------------*/
	wMButton4 = new STTButton(p_rt, this, "wMButton4", 0);
	wMButton4->setMainArg(399, 53, 198, 48, false);
	wMButton4->setPressedArg("play", 0x0, "文泉驿微米黑", 16, 0);
	wMButton4->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton4->setReleasedArg("play", 0x0, "文泉驿微米黑", 16, 0);
	wMButton4->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton4->onEventHandler(Event::CLICKED, this, (EHandler) & Frm02::wMButton4_clk_cb);

	/*called after widget created.*/

	return; //createParts END
}

#include "../src/onvif.h"
static struct OnvifSession *onvif_session;
static vector<struct OnvifData *> vecdata;

void Frm02::onLoad() {
	onvif_session = (struct OnvifSession*)malloc(sizeof(struct OnvifSession));
	initializeSession(onvif_session);
}

bool Frm02::onClosing() {
	return true;
}

void Frm02::onDispose() {
	closeSession(onvif_session);
	for(auto onvif_data : vecdata) {
		free(onvif_data);
	}
	free(onvif_session);
}



void Frm02::wMButton2_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton1（功能键）的点击事件*/
	int number_of_cameras = broadcast(onvif_session);
	fprintf(stderr, "libonvif found %d cameras\n", number_of_cameras);
	for (int i = 0; i<number_of_cameras; i++) {
		struct OnvifData *onvif_data = (struct OnvifData*)malloc(sizeof(struct OnvifData));
		vecdata.push_back(onvif_data);
		prepareOnvifData(i, onvif_session, onvif_data);
		strcpy(onvif_data->username,"test");
		strcpy(onvif_data->password,"whxtt2022");
		fillRTSP(onvif_data);

//		wMText1->addText(to_string(i+1));
//		wMText1->addText("\r\n");
//		wMText1->addText(onvif_data->camera_name);
//		wMText1->addText("\r\n");
//		wMText1->addText(onvif_data->xaddrs);
//		wMText1->addText("\r\n");
//		cout<<i+1<<endl;
//		cout<<onvif_data->camera_name<<endl;
//		cout<<onvif_data->xaddrs<<endl;
//
//		wMText1->addText(onvif_data->stream_uri);
//		cout<<onvif_data->stream_uri<<endl;
		string url =onvif_data->stream_uri;// "rtsp://test:whxtt2022@";
		url ="rtsp://test:whxtt2022@" + url.substr(7);
		wMLabel1->setText(url);

	}

}

void Frm02::wMButton3_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton1（功能键）的点击事件*/
	continuousMove(wMSlider1->getValue()/100.0,wMSlider2->getValue()/100.0,wMSlider3->getValue()/100.0,vecdata[0]);
}

void Frm02::wMButton4_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton1（功能键）的点击事件*/
	string url = wMLabel1->getText();
	cout<<url<<endl;
	wMVideo1->setSrc(url.c_str());
	wMVideo1->play();
}
